package com.balance.anole.erp.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.balance.anole.common.to.CodeLookTO;
import com.balance.anole.erp.util.ServicesCenter;

public class BizCodesService {

	private static BizCodesService instance;

	private Map<String, List<CodeLookTO>> codesMap;

	private static com.balance.anole.common.service.CommonService commonService = 
		ServicesCenter.getServicesCenter().getService(com.balance.anole.common.service.CommonService.class);
	private static com.balance.anole.client.common.service.CommonService clientCommonService = 
		ServicesCenter.getServicesCenter().getService(com.balance.anole.client.common.service.CommonService.class);
	

	private BizCodesService(){
		codesMap = new HashMap<String, List<CodeLookTO>>();
	}

	public static BizCodesService getInstance(){
		if(instance == null){
			instance = new BizCodesService();
		}
		return instance;
	}

	public String[] getCodeDescsByCodeType(String selectionType){
		return getCodeDescsByCodeType(selectionType,false);
	}
	
	public String[] getCodeDescsByCodeType(String selectionType, boolean isClient){
		List<CodeLookTO> result = codesMap.get(selectionType);
		if(result == null){
			List<CodeLookTO> codes;
			if(isClient){
				codes = clientCommonService.getCodeLooksByCodeTypes(new String[]{selectionType}).get(selectionType);
			}else{
				codes = commonService.getCodeLooksByCodeTypes(new String[]{selectionType}).get(selectionType);
			}
			String[] descs = new String[codes.size()];
			int i = 0;
			for (CodeLookTO codeLookTO : codes) {
				descs[i] = codeLookTO.getCodeDesc();
				i++;
			}
			codesMap.put(selectionType, result);
			return descs;
		}
		return result.toArray(new String[0]);
	}

	public List<CodeLookTO> getCodeLooksByCodeType(String selectionType){
		return getCodeLooksByCodeType(selectionType,false);
	}
	
	public List<CodeLookTO> getCodeLooksByCodeType(String selectionType,boolean isClient){
		List<CodeLookTO> result = codesMap.get(selectionType);
		if(result == null){
			if(isClient){
				result = clientCommonService.getCodeLooksByCodeTypes(new String[]{selectionType}).get(selectionType);
			}else{
				result = commonService.getCodeLooksByCodeTypes(new String[]{selectionType}).get(selectionType);
			}
			codesMap.put(selectionType, result);
		}
		return result;
	}

	public String getCodeDesc(String selectionType, String codeId){
		return getCodeDesc(selectionType,codeId,false);
	}
	
	public String getCodeDesc(String selectionType, String codeId, boolean isClient){
		List<CodeLookTO> codes;
		if(isClient){
			codes = getCodeLooksByCodeType(selectionType,isClient);
		}else{
			codes = getCodeLooksByCodeType(selectionType);
		}
		
		String desc = null;
		for (CodeLookTO codeLookTO : codes) {
			if(codeLookTO.getCodeId().equalsIgnoreCase(codeId)){
				desc = codeLookTO.getCodeDesc();
				break;
			}
		}
		return desc;
	}

}
